﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GameRobot;
using System.Reflection;

namespace GameRobotControl
{
    public class SchedulingRobotAction :RobotAction
    {
        List<MethodTag> _listMethod;
        List<MethodTag> _listTag;
        protected VirtualRobot _virRobot;

        public SchedulingRobotAction(RobotBridge robotbride)
            : base(robotbride)
        {
            _listMethod = new List<MethodTag>();
            _listTag = new List<MethodTag>();
        }

        public void InitRobot(string locationname, VirtualRobot tern)
        {
            List<VirtualRobot> _listVirRobot = LoadRobot("Sample");
            if (_listVirRobot != null)
            {
                _virRobot = _listVirRobot[0];
                GetBaseFunction(tern, _virRobot, _listMethod);
                GetMetaFunction(_virRobot, _listMethod);
            }
            else
            {
                _virRobot = tern;
                GetBaseFunction(tern, _virRobot, _listMethod);
            }
            _virRobot._controlType = ControlType.SchedulingControl;
        }

        #region get list method
        public List<string> GetListBaseMethod()
        {
            List<string> listName = new List<string>();
            for (int j = 0; j < _listMethod.Count; j++)
            {
                if (_listMethod[j]._method.DeclaringType.Name != _virRobot.GetType().Name)
                {
                    string name = j + " ";
                    name += _listMethod[j]._method.Name.Replace("_sd", "");
                    name += "(";
                    ParameterInfo[] paramInfos = _listMethod[j]._method.GetParameters();
                    for (int i = 0; i < paramInfos.Length; i++)
                    {
                        if (i < paramInfos.Length - 1)
                            name += paramInfos[i].ParameterType.Name + ",";
                        else
                            name += paramInfos[i].ParameterType.Name;
                    }
                    name += ")";
                    listName.Add(name);
                }
            }
            return listName;
        }

        public List<string> GetListMetaMethod()
        {
            List<string> listName = new List<string>();
            for (int j = 0; j < _listMethod.Count; j++)
            {
                if (_listMethod[j]._method.DeclaringType.Name == _virRobot.GetType().Name)
                {
                    string name = j + " ";
                    name += _listMethod[j]._method.Name.Replace("_sd", "");
                    name += "(";
                    ParameterInfo[] paramInfos = _listMethod[j]._method.GetParameters();
                    for (int i = 0; i < paramInfos.Length; i++)
                    {
                        if (i < paramInfos.Length - 1)
                            name += paramInfos[i].ParameterType.Name + ",";
                        else
                            name += paramInfos[i].ParameterType.Name;
                    }
                    name += ")";
                    listName.Add(name);
                }
            }
            return listName;
        }
        #endregion

        public List<string> GetListParam(int idmethod)
        {
            ParameterInfo[] paramInfos = _listMethod[idmethod]._method.GetParameters();
            List<string> paramType = new List<string>();
            for (int i = 0; i < paramInfos.Length; i++)
            {
                paramType.Add(paramInfos[i].ParameterType.Name);
            }
            return paramType;
        }

        public void ResetControl()
        {
            _listTag.Clear();
        }

        public int AddControl(int id, List<object> listparam)
        {
            int i = 0;
            bool ok = true;
            try
            {
                List<object> parameters = null;
                if (listparam != null)
                {
                    parameters = new List<object>();
                    ParameterInfo[] parameterInfos = _listMethod[id]._method.GetParameters();
                    for (i = 0; i < parameterInfos.Length; i++)
                    {
                        Type type = parameterInfos[i].ParameterType;
                        // kiem tra du lieu dau vao ok
                        parameters.Add(Convert.ChangeType(listparam[i], type));
                    }
                }

                MethodTag newtag = new MethodTag();
                newtag._master = _listMethod[id]._master;
                newtag._method = _listMethod[id]._method;
                if (parameters != null)
                    newtag._params = parameters.ToArray();
                else
                    newtag._params = null;

                _listTag.Add(newtag);
            }
            catch (Exception)
            {
                ok = false;
            }

            if (!ok)
                return i;
            return -1;
        }

        public int GetIDTag()
        {
            return _virRobot._IDTag;
        }

        public bool IsAutoControl()
        {
            return _virRobot.IsActive();
        }

        public override void Start()
        {
            _virRobot.SetMethods(_listTag);
            _virRobot.Start();
        }
    }
}
